package com.itheima.health.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.health.common.MessageConst;
import com.itheima.health.entity.Result;
import com.itheima.health.service.MenuService;
import com.itheima.health.service.UserService;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @autahor: csl
 * @date: 2019/7/27
 * @description:
 * @version:1.0
 */
@RestController
@RequestMapping("/user")
public class UserController {
    @Reference
    private MenuService menuService;
    @Reference
    private UserService service;
    @RequestMapping("/login")
    public Result login(String username,String password){
         try {
                   if ( service.login(username,password)){
                       return new Result(true,MessageConst.LOGIN_SUCCESS);
                   }else {
                       return new Result(false,"登录失败");
                   }
                 }catch (Exception e){
                     e.printStackTrace();
                       return new Result(false,"登录失败");
                 }
    }
    @RequestMapping("/loginSuccess")
    public Result loginSuccess(){

        return new Result(true, MessageConst.LOGIN_SUCCESS);
    }
    @RequestMapping("/loginFail")
    public Result loginFail(){
        return new Result(false,"登录失败");
    }
    @RequestMapping("/getUsername")
    public Result getUsername(){
         try {
             Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
             User user = (User) authentication.getPrincipal();
             String username = user.getUsername();

             return new Result(true,MessageConst.ACTION_SUCCESS,username);
         }catch (Exception e){
                     e.printStackTrace();
             return new Result(true,MessageConst.ACTION_FAIL);

                 }
    }
    @RequestMapping("/findMenuListByusername")
    public Result findMenuListByusername(String username){
         try {
             System.out.println(username);
                   List<Map<String,Object>>list= menuService.findMenuListByusername(username);
                  Map<String,Object>map= new HashMap<>();
                  /*
                    "path": "1",
                    "title": "工作台",
                    "icon":"fa-dashboard",
                    "children": []
                   */
                  map.put("paht",1);
                  map.put("title","工作台");
                  map.put("icon","dashboard");
                  map.put("children",new ArrayList());
                  list.add(map);
                   return new Result(true,MessageConst.ACTION_SUCCESS,list);
                 }catch (Exception e){
                     e.printStackTrace();
                     return new Result(false,MessageConst.ACTION_FAIL);
                 }
    }
}
